/**
 * @description 处理文件（视频、图片）至对应的值中
 * @description imageType图片类型1:'订单' 2: '卡牌'
 * @description position图片型号 1:正面图片2：背面图片3：贴纸拍照4:卡牌图片
 * @description type图片型号 1:图片 2:视频
 */
export default function formatterFile (fileInfos) {
  if (!fileInfos || !Array.isArray(fileInfos)) return null
  let videoFile = null // 视频
  let pictureList = null // 卡牌大合照
  let frontImage = null // 卡牌正面扫描
  let backImage = null // 卡牌背面扫描
  let stickerPhoto = null // 卡牌贴标正面拍照

  fileInfos.forEach(file => {
    const {cardUid, id, imageType, imageUrl, position, type} = file
    // 处理视频情况
    if (type === 2) {
      videoFile = {
        id,
        url: imageUrl
      }
    } else {
      const imageInfo = {
        id,
        uid: cardUid,
        url: imageUrl
      }
      // 图片
      switch (position) {
      case 1: // 正面图片
        frontImage = imageInfo
        break
      case 2: // 背面图片
        backImage = imageInfo
        break
      case 3: // 贴纸拍照
        stickerPhoto = imageInfo
        break
      case 4: // 卡牌图片
        if (!pictureList) pictureList = []
        pictureList.push(imageInfo)
        break
      default:
        return
      }
    }
  })

  // 如果pictureList有值，但不够5个情况，补空数据
  if (Array.isArray(pictureList) && pictureList.length < 5) {
    const voidLength = 5 - pictureList.length
    for (let i = 0; i < voidLength; i++) {
      pictureList.push({ uid: 0, url: '', id: '' })
    }
  }

  return {
    videoFile,
    pictureList,
    frontImage,
    backImage,
    stickerPhoto
  }
}

// 将数据处理成接口适配结构
export const fileToUpload = ({frontImage, backImage, stickerPhoto, videoFile, pictureList}, trackingNo = '') => {
  const front = frontImage && {
    cardUid: frontImage.uid,
    position: 1,
    imageUrl: frontImage.url,
    imageId: frontImage.id,
    imageType: 2,
    type: 1
  }
  const back = backImage && {
    cardUid: backImage.uid,
    position: 2,
    imageUrl: backImage.url,
    imageId: backImage.id,
    imageType: 2,
    type: 1
  }
  const sticker = stickerPhoto && {
    cardUid: stickerPhoto.uid,
    position: 3,
    imageUrl: stickerPhoto.url,
    imageId: stickerPhoto.id,
    imageType: 2,
    type: 1
  }
  const video = videoFile && {
    imageUrl: videoFile.url,
    imageId: videoFile.id,
    imageType: 1,
    type: 2,
    trackingNo
  }
  let pictures = pictureList && pictureList.filter(p => p.id).map(p => {
    return {
      cardUid: p.uid,
      position: 4,
      imageUrl: p.url,
      imageId: p.id,
      imageType: 2,
      type: 1,
      trackingNo
    }
  }) || []

  return [front, back, sticker, video, ...pictures].filter(Boolean)
}
// 为卡牌大合照数据集合集成uid，做上传图片可用
export const setUidByPictureList = (list, uid) => {
  return list.map(item => ({
    ...item,
    uid
  }))
}
// 卡牌大合照默认数据
export const defaultPictureList = [{ uid: 0, url: '', id: '' }, { uid: 0, url: '', id: '' }, { uid: 0, url: '', id: '' }, { uid: 0, url: '', id: '' }, { uid: 0, url: '', id: '' }]
